package seqlist.stack;

import java.util.ArrayList;
import java.util.List;
import java.util.NoSuchElementException;

public class Mystack<E> {
    private int size;
    private List<E>data= new ArrayList<>();

    public void push(E val){
        data.add(val);
        size++;
    }
    public E pop(){
        if (isEmpty()){
            throw new NoSuchElementException("stack is empty!cannot pop!");
        }
        E val = data.remove(size-1);
        size--;
        return val;
    }
    public E peek(){
        if (isEmpty()){
            throw new NoSuchElementException("stack is empty!cannot peek!");
        }
        return data.get(size-1);
    }

    @Override
    public String toString() {
      StringBuilder sb=new StringBuilder();
      sb.append("[");
        for (int i = 0; i < size; i++) {
            sb.append(data.get(i));
            if (i!=size-1){
                sb.append(",");
            }
        }
        sb.append("]top");
        return toString();
    }

    private boolean isEmpty() {
        return size == 0;
    }
}
